package org.example.rabbitmq.concurrent.lock;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;

public class JsonTest {
    public static void main(String[] args) throws JsonProcessingException {
        test_case_insensitive();
        test_snake_style();
    }

    private static void test_case_insensitive() throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
        String json = "{\"userid\":\"aaa\",\"username\":\"张三\"}";
        User user = objectMapper.readValue(json, User.class);
        System.out.println(user);

        String json2 = "{\"USERID\":\"aaa\",\"USERname\":\"张三\"}";
        User user2 = objectMapper.readValue(json, User.class);
        System.out.println(user2);
    }

    private static void test_snake_style() throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
        String json = "{\"user_id\":\"aaa\",\"user_name\":\"张三\"}";
        User user = objectMapper.readValue(json, User.class);
        System.out.println(user);
    }

    public static class User {
        private String userId;
        private String userName;

        public String getUserId() {
            return userId;
        }

        public void setUserId(String userId) {
            this.userId = userId;
        }

        public String getUserName() {
            return userName;
        }

        public void setUserName(String userName) {
            this.userName = userName;
        }

        @Override
        public String toString() {
            return userId + " " + userName;
        }
    }
}
